﻿using Microsoft.AspNetCore.Mvc;
using JESAI.Application.Abstractions.Test;
using Newtonsoft.Json;
using JESAI.Biz.Data.Enums;
using JESAI.Caching;
using JESAI.Application.Abstractions.Sys;
using JESAI.AutoMapper.ObjectMapping;
using JESAI.Framework.Mvc;
using AspectCore.DynamicProxy.Parameters;
using JESAI.Application.Abstractions;
using JESAI.Framework.Attributes;
using System.Diagnostics;
using JESAI.Core.Util.Threading.Asyncs;
using JESAI.Core.Configuration;

namespace JESAI.Platform.WebApi.Controllers
{
    [ApiController]
    [Route("api/v1/[controller]/[action]/{method}")]
    public class TestController : ApiControllerBase
    {
        private readonly ITestApplicationService _testApplicationService;
        private CacheManager _cacheManager;
        private readonly ILogger<TestController> _logger;
        private readonly IScheduleService _scheduleService;
        private readonly IHostApplicationLifetime _applicationLifetime;
        public TestController(
            ITestApplicationService testApplicationService,
            ILogger<TestController> logger,
            CacheManager cacheManager,
            IScheduleService scheduleService,
            IHostApplicationLifetime applicationLifetime)
        {
            _testApplicationService = testApplicationService;
            _logger = logger;
            _cacheManager = cacheManager;
            _scheduleService = scheduleService;
            _applicationLifetime = applicationLifetime;
            //EEGDataEdfWriterSubsciber<double[], EEGDataEvent> eEGDataEdfWriterSubsciber = new EEGDataEdfWriterSubsciber<double[],EEGDataEvent>();
            //eEGDataEdfWriterSubsciber.Subscribe((data) => 
            //{
            //    Console.WriteLine(data);
            //    throw new Exception("222");
            //});
            //logger.LogError("测试");
            //EEGDataLSLSubsciber<double[], EEGDataEvent> eEGDataLSLSubsciber = new EEGDataLSLSubsciber<double[],EEGDataEvent>();
            //eEGDataLSLSubsciber.Subscribe((data) =>
            //{
            //    Console.WriteLine(data);
            //});
        }
        [HttpGet(Name = "AuthorizationGet")]
        [WebApiAuthorization]
        public IActionResult AuthorizationGet()
        {
            return Success("恭喜你,接口鉴权功能已经对接完成");
        }

        [HttpPost(Name = "AuthorizationPost")]
        [WebApiAuthorization]
        public IActionResult AuthorizationPost()
        {
            return Success("恭喜你,接口鉴权功能已经对接完成");
        }

        [HttpGet(Name = "Index")]
        public string Index()
        {
            var test = _testApplicationService.GetAllTest();
            return JsonConvert.SerializeObject(test);
        }

        [HttpGet(Name = "TestString")]
        public string TestString(string method)
        {           
            //获取请求头参数
            return method + "2323232";
        }

        [HttpPost(Name = "TestStringP")]
        public string TestStringP(string method)
        {
            //获取请求头参数
            return method + "2323232";
        }

        [HttpGet(Name = "TestCodeRun")]
        public async Task<string> TestCodeRun()
        {

            return await _testApplicationService.TestCodeRun();
        }

        [HttpGet(Name = "CacheTest")]
        public string CacheTest()
        {
            _cacheManager.GetOrAdd("Test", (val) =>
            {

                return "33333";
            });

            return _cacheManager.Get<string>("Test");
        }

        [HttpGet(Name = "ScheduleJobTest")]
        public string ScheduleJobTest()
        {
            _scheduleService.StartAllAutoStartJob();
            return "44444";
        }


        [HttpGet(Name = "Restart")]
        public IActionResult Restart()
        {
            //// 获取当前应用程序的进程ID
            //int currentProcessId = Process.GetCurrentProcess().Id;
            //// 获取当前应用程序的可执行文件路径
            //string currentExecutablePath = Process.GetCurrentProcess().MainModule.FileName;
            //// 停止当前应用程序的进程
            //Process.GetCurrentProcess().Kill();
            //// 启动一个新的进程来重新运行应用程序
            //Process.Start(currentExecutablePath);

            _applicationLifetime.StopApplication();
            // 返回重启成功的消息
            return Success("Application restarted");
        }

        [HttpGet(Name = "MqttTest")]
        public string MqttTest()
        {
            //AsyncHelper.RunSync(async () =>
            //{
            //   await MQTTMessagePublish.Publish_Message(new MqttMessage()
            //    {
            //        Message = "22222222222222222222222",
            //        Topic = "test",
            //    });

            //    return Task.CompletedTask;
            //});
            return "22222222";
        }
    }
}
